package deng.qiya.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import deng.qiya.model.ReplyMessage;
import deng.qiya.util.CustomerContextHolder;
import deng.qiya.wechat.Constant;

public class ReplyMessageOper {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public int newReplyMessage(ReplyMessage replyMessage){
		
		String str = "INSERT INTO [REPLY_MESSAGE]           ([REPLY_ID]           ,[MESSAGE_TYPE]           ,[REPLY_CONTENT]           ,[REPLY_TIME]) VALUES(?,?,?,?)";
		CustomerContextHolder.setCustomerType(Constant.ds);
		Object[] params = new Object[]{replyMessage.getReplyId(),replyMessage.getMessageType(),replyMessage.getReplyContent(),replyMessage.getReplyTime()};
		jdbcTemplate.update(str, params);
		return jdbcTemplate.queryForInt("SELECT @@IDENTITY");
	}
	
	public ReplyMessage getReplyMessageByReplyId(String replyId){
		String sql = "SELECT [ID]      ,[REPLY_ID]      ,[MESSAGE_TYPE]      ,[REPLY_CONTENT]      ,[REPLY_TIME]  FROM [REPLY_MESSAGE] WHERE REPLY_ID=?";
		CustomerContextHolder.setCustomerType(Constant.ds);
		Object[] params = new Object[]{replyId};
		List<ReplyMessage> list = jdbcTemplate.query(sql,params, new ReplyMessageMapper());
		if(list != null && list.size()>0){
			return list.get(0);
		}
		else{
			return null;
		}
	}
	

	
	
	
	
	
	
	
	class ReplyMessageMapper implements RowMapper<ReplyMessage>{

		@Override
		public ReplyMessage mapRow(ResultSet rs, int rowNum) throws SQLException {
			ReplyMessage message = new ReplyMessage();
				message.setId(new Integer(rs.getString("ID")));
				message.setMessageType(rs.getString("MESSAGE_TYPE"));
				message.setReplyContent(rs.getString("REPLY_CONTENT"));
				message.setReplyId(new Integer(rs.getString("REPLY_ID")));
				message.setReplyTime(rs.getDate("REPLY_TIME"));;
			return message;
		}
		
	}
	
}
